import { LogHelper } from "../utils/logHelper";

/**
 * 添加至孵化队列
 */
Room.prototype.spawnQueueAdd = function(role:string,body:BodyPartConstant[],memory?:CreepMemory,priority?:boolean){
  if(!this.memory.spawnQueue){
    this.memory.spawnQueue = new Array<{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory ,priority?:boolean}>();
  }

  if(!memory){
    memory = { role,bornRoom:this.name };
  }

  this.memory.spawnQueue.push({ id:Game.time,role,body,memory,priority:priority });
}

/**
 *  孵化队列删除指定元素
 */
Room.prototype.spawnQueueRemove = function(creep:{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory,priority?:boolean }){
  const index = this.memory.spawnQueue.findIndex(c => c.id === creep.id);
  if (index !== -1) {
    this.memory.spawnQueue.splice(index, 1);
  }
}

/**
 * 孵化队列是否存在指定角色
 */
Room.prototype.spawnQueueGetRole = function(role:string,filterFunc:(creep:{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory,priority?:boolean }) => boolean):Array<{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory,priority?:boolean }>{
  if(!this.memory.spawnQueue){
    this.memory.spawnQueue = new Array<{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory,priority?:boolean }>();
  }
  return this.memory.spawnQueue
    .filter((creep) => creep.role === role)
    .filter(filterFunc);
}

/**
 * 获取房间内的spawn
 */
Room.prototype.spawn = function():StructureSpawn | null{
  if(this.memory.spawn){
    return Game.getObjectById(this.memory.spawn);
  }
  else {
    const spawns = this.find(FIND_MY_SPAWNS);
    if(spawns.length > 0){
      return spawns[0];
    }
  }
  return null;
}

/**
 * 动态计算所需Body
 */
Room.prototype.calculateCreepBody = function(partRatios: { [part: string]: number },energyCapacityAvailable?:number):BodyPartConstant[] {
  let energyCapacity: number = this.energyCapacityAvailable;
  const body: BodyPartConstant[] = [];
  if(energyCapacityAvailable){
    energyCapacity = energyCapacityAvailable;
  }
  for (const part in partRatios) {
    if (partRatios.hasOwnProperty(part)) {
      const ratio: number = partRatios[part];
      let numParts: number;
      switch (part) {
        case MOVE:
          numParts = Math.floor((energyCapacity * ratio) / 50);
          break;
        case WORK:
          numParts = Math.floor((energyCapacity * ratio) / 100);
          break;
        case CARRY:
          numParts = Math.floor((energyCapacity * ratio) / 50);
          break;
        case ATTACK:
          numParts = Math.floor((energyCapacity * ratio) / 80);
          break;
        case RANGED_ATTACK:
          numParts = Math.floor((energyCapacity * ratio) / 150);
          break;
        case HEAL:
          numParts = Math.floor((energyCapacity * ratio) / 250);
          break;
        case TOUGH:
          numParts = Math.floor((energyCapacity * ratio) / 10);
          break;
        case CLAIM:
          numParts = Math.floor((energyCapacity * ratio) / 600);
          break;
        default:
          numParts = 0;
          break;
      }

      for (let i = 0; i < numParts; i++) {
        body.push(part as BodyPartConstant);
      }
    }
  }

  // 如果部件数量超过50个，减少一个最多的部件，直到部件数量小于等于50
  while (body.length > 50) {
    const partCounts: { [part: string]: number } = {}; // 用于跟踪部件数量
    body.forEach((part: BodyPartConstant) => {
      partCounts[part] = partCounts[part] ? partCounts[part] + 1 : 1;
    });

    let maxPart: BodyPartConstant | null = null;
    let maxCount: number = 0;

    // 找到数量最多的部件
    for (const part in partCounts) {
      if (partCounts.hasOwnProperty(part)) {
        const count = partCounts[part];
        if (count > maxCount) {
          maxPart = part as BodyPartConstant;
          maxCount = count;
        }
      }
    }

    // 从 body 数组中删除一个最多的部件
    if(maxPart){
      const index = body.indexOf(maxPart);
      if (index !== -1) {
        body.splice(index, 1);
      }
    }
  }

  return body;
}

/**
 * 判断房间是不是我的
 */
Room.prototype.my = function():boolean{
  const controller = this.controller;
  if(controller){
    return controller.my;
  }
  return false;
}